{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b97ddf3b-d921-4169-9d82-b0ee95e9eccd",
   "metadata": {},
   "source": [
    "# Iterative Prompt Development\n",
    "In this lesson, you'll iteratively analyze and refine your prompts to generate marketing copy from a product fact sheet.\n",
    "\n",
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2800e646",
   "metadata": {
    "height": 132,
    "tags": []
   },
   "outputs": [],
   "source": [
    "import openai\n",
    "import os\n",
    "\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "_ = load_dotenv(find_dotenv()) # read local .env file\n",
    "\n",
    "openai.api_key  = os.getenv('OPENAI_API_KEY')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9fb6f2de",
   "metadata": {
    "height": 149,
    "tags": []
   },
   "outputs": [],
   "source": [
    "def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n",
    "    messages = [{\"role\": \"user\", \"content\": prompt}]\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model=model,\n",
    "        messages=messages,\n",
    "        temperature=0, # this is the degree of randomness of the model's output\n",
    "    )\n",
    "    return response.choices[0].message[\"content\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffc2b32f-73da-4ef0-81f6-0c7bff70783d",
   "metadata": {},
   "source": [
    "**Note**: In June 2023, OpenAI updated gpt-3.5-turbo. The results you see in the notebook may be slightly different than those in the video. Some of the prompts have also been slightly modified to product the desired results."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71d1a1df-03c3-4f38-af6d-3962362f3dcd",
   "metadata": {},
   "source": [
    "## Generate a marketing product description from a product fact sheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5be90e47",
   "metadata": {
    "height": 693,
    "tags": []
   },
   "outputs": [],
   "source": [
    "fact_sheet_chair = \"\"\"\n",
    "OVERVIEW\n",
    "- Part of a beautiful family of mid-century inspired office furniture, \n",
    "including filing cabinets, desks, bookcases, meeting tables, and more.\n",
    "- Several options of shell color and base finishes.\n",
    "- Available with plastic back and front upholstery (SWC-100) \n",
    "or full upholstery (SWC-110) in 10 fabric and 6 leather options.\n",
    "- Base finish options are: stainless steel, matte black, \n",
    "gloss white, or chrome.\n",
    "- Chair is available with or without armrests.\n",
    "- Suitable for home or business settings.\n",
    "- Qualified for contract use.\n",
    "\n",
    "CONSTRUCTION\n",
    "- 5-wheel plastic coated aluminum base.\n",
    "- Pneumatic chair adjust for easy raise/lower action.\n",
    "\n",
    "DIMENSIONS\n",
    "- WIDTH 53 CM | 20.87”\n",
    "- DEPTH 51 CM | 20.08”\n",
    "- HEIGHT 80 CM | 31.50”\n",
    "- SEAT HEIGHT 44 CM | 17.32”\n",
    "- SEAT DEPTH 41 CM | 16.14”\n",
    "\n",
    "OPTIONS\n",
    "- Soft or hard-floor caster options.\n",
    "- Two choices of seat foam densities: \n",
    " medium (1.8 lb/ft3) or high (2.8 lb/ft3)\n",
    "- Armless or 8 position PU armrests \n",
    "\n",
    "MATERIALS\n",
    "SHELL BASE GLIDER\n",
    "- Cast Aluminum with modified nylon PA6/PA66 coating.\n",
    "- Shell thickness: 10 mm.\n",
    "SEAT\n",
    "- HD36 foam\n",
    "\n",
    "COUNTRY OF ORIGIN\n",
    "- Italy\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4f656d0c",
   "metadata": {
    "height": 234,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Introducing our stylish and versatile mid-century inspired office chair, perfect for both home and business settings. This chair is part of a beautiful family of office furniture that includes filing cabinets, desks, bookcases, meeting tables, and more.\n",
      "\n",
      "Customize your chair with several options of shell color and base finishes to suit your personal style. Choose between plastic back and front upholstery or full upholstery in a variety of fabric and leather options. The base finish options include stainless steel, matte black, gloss white, or chrome. You can also choose to have armrests or go for a sleek armless design.\n",
      "\n",
      "Constructed with a 5-wheel plastic coated aluminum base, this chair features a pneumatic chair adjust for easy raise/lower action. The dimensions of the chair are as follows: width 53 cm, depth 51 cm, height 80 cm, seat height 44 cm, and seat depth 41 cm.\n",
      "\n",
      "Customize your chair further with options such as soft or hard-floor caster options, two choices of seat foam densities (medium or high), and armless or 8 position PU armrests.\n",
      "\n",
      "Made with high-quality materials, the shell base glider is constructed with cast aluminum with modified nylon PA6/PA66 coating, while the seat features HD36 foam. This chair is designed and manufactured in Italy, ensuring both style and quality.\n",
      "\n",
      "Elevate your workspace with our stylish and functional mid-century inspired office chair, designed to enhance your comfort and productivity.\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Your task is to help a marketing team create a \n",
    "description for a retail website of a product based \n",
    "on a technical fact sheet.\n",
    "\n",
    "Write a product description based on the information \n",
    "provided in the technical specifications delimited by \n",
    "triple backticks.\n",
    "\n",
    "Technical specifications: ```{fact_sheet_chair}```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1361d6f-6022-4b7a-8107-b2e2ff5efd32",
   "metadata": {},
   "source": [
    "## Issue 1: The text is too long \n",
    "- Limit the number of words/sentences/characters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f6129532",
   "metadata": {
    "height": 285,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Introducing our versatile and stylish mid-century office chair, available in a variety of colors and finishes. With adjustable height and comfortable upholstery options, this chair is perfect for both home and business use. Made with quality materials from Italy, it's a perfect blend of form and function.\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Your task is to help a marketing team create a \n",
    "description for a retail website of a product based \n",
    "on a technical fact sheet.\n",
    "\n",
    "Write a product description based on the information \n",
    "provided in the technical specifications delimited by \n",
    "triple backticks.\n",
    "\n",
    "Use at most 50 words.\n",
    "\n",
    "Technical specifications: ```{fact_sheet_chair}```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "17f658d0",
   "metadata": {
    "height": 30,
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "47"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(response.split())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6de9c92-857c-4d1c-8fd1-192cf47c4763",
   "metadata": {},
   "source": [
    "## Issue 2. Text focuses on the wrong details\n",
    "- Ask it to focus on the aspects that are relevant to the intended audience."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "954a8a06",
   "metadata": {
    "height": 336,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Introducing our versatile and stylish office chair, part of a mid-century inspired furniture collection. Choose from a variety of shell colors and base finishes to suit your space. Constructed with a durable aluminum base and high-density foam seat for comfort. Made in Italy, perfect for home or business use.\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Your task is to help a marketing team create a \n",
    "description for a retail website of a product based \n",
    "on a technical fact sheet.\n",
    "\n",
    "Write a product description based on the information \n",
    "provided in the technical specifications delimited by \n",
    "triple backticks.\n",
    "\n",
    "The description is intended for furniture retailers, \n",
    "so should be technical in nature and focus on the \n",
    "materials the product is constructed from.\n",
    "\n",
    "Use at most 50 words.\n",
    "\n",
    "Technical specifications: ```{fact_sheet_chair}```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e9455d58",
   "metadata": {
    "height": 387,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Introducing our versatile and stylish office chair, part of a mid-century inspired collection. Choose from a variety of shell colors and base finishes to suit your space. Constructed with a durable aluminum base and high-density foam seat for comfort. Perfect for home or business use. Product ID: SWC-100, SWC-110.\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Your task is to help a marketing team create a \n",
    "description for a retail website of a product based \n",
    "on a technical fact sheet.\n",
    "\n",
    "Write a product description based on the information \n",
    "provided in the technical specifications delimited by \n",
    "triple backticks.\n",
    "\n",
    "The description is intended for furniture retailers, \n",
    "so should be technical in nature and focus on the \n",
    "materials the product is constructed from.\n",
    "\n",
    "At the end of the description, include every 7-character \n",
    "Product ID in the technical specification.\n",
    "\n",
    "Use at most 50 words.\n",
    "\n",
    "Technical specifications: ```{fact_sheet_chair}```\n",
    "\"\"\"\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5be496d5-54c9-443b-8116-0b3d3d9dc560",
   "metadata": {},
   "source": [
    "## Issue 3. Description needs a table of dimensions\n",
    "- Ask it to extract information and organize it in a table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0384f1e4",
   "metadata": {
    "height": 540,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div>\n",
      "<p>Introducing our latest addition to the mid-century inspired office furniture collection - the versatile and stylish office chair. This chair is designed to elevate any workspace, whether it be at home or in a business setting. With a variety of shell colors and base finishes to choose from, you can customize this chair to fit your aesthetic perfectly.</p>\n",
      "\n",
      "<p>The construction of this chair is top-notch, featuring a 5-wheel plastic coated aluminum base for stability and mobility. The pneumatic chair adjust allows for easy raise and lower action, ensuring maximum comfort throughout the day.</p>\n",
      "\n",
      "<p>When it comes to materials, this chair is built to last. The shell base glider is made of cast aluminum with a modified nylon PA6/PA66 coating, while the seat is crafted from HD36 foam for ultimate comfort and durability.</p>\n",
      "\n",
      "<p>Don't compromise on style or functionality - choose our mid-century inspired office chair for your workspace today.</p>\n",
      "\n",
      "<p>Product IDs: SWC-100, SWC-110</p>\n",
      "</div>\n",
      "\n",
      "<table>\n",
      "  <caption>Product Dimensions</caption>\n",
      "  <tr>\n",
      "    <td>WIDTH</td>\n",
      "    <td>20.87\"</td>\n",
      "  </tr>\n",
      "  <tr>\n",
      "    <td>DEPTH</td>\n",
      "    <td>20.08\"</td>\n",
      "  </tr>\n",
      "  <tr>\n",
      "    <td>HEIGHT</td>\n",
      "    <td>31.50\"</td>\n",
      "  </tr>\n",
      "  <tr>\n",
      "    <td>SEAT HEIGHT</td>\n",
      "    <td>17.32\"</td>\n",
      "  </tr>\n",
      "  <tr>\n",
      "    <td>SEAT DEPTH</td>\n",
      "    <td>16.14\"</td>\n",
      "  </tr>\n",
      "</table>\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "Your task is to help a marketing team create a \n",
    "description for a retail website of a product based \n",
    "on a technical fact sheet.\n",
    "\n",
    "Write a product description based on the information \n",
    "provided in the technical specifications delimited by \n",
    "triple backticks.\n",
    "\n",
    "The description is intended for furniture retailers, \n",
    "so should be technical in nature and focus on the \n",
    "materials the product is constructed from.\n",
    "\n",
    "At the end of the description, include every 7-character \n",
    "Product ID in the technical specification.\n",
    "\n",
    "After the description, include a table that gives the \n",
    "product's dimensions. The table should have two columns.\n",
    "In the first column include the name of the dimension. \n",
    "In the second column include the measurements in inches only.\n",
    "\n",
    "Give the table the title 'Product Dimensions'.\n",
    "\n",
    "Format everything as HTML that can be used in a website. \n",
    "Place the description in a <div> element.\n",
    "\n",
    "Technical specifications: ```{fact_sheet_chair}```\n",
    "\"\"\"\n",
    "\n",
    "response = get_completion(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d681db67-6d59-40c1-a36b-4d76729a690d",
   "metadata": {},
   "source": [
    "## Load Python libraries to view HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "73ff0052",
   "metadata": {
    "height": 30,
    "tags": []
   },
   "outputs": [],
   "source": [
    "from IPython.display import display, HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3465ffce",
   "metadata": {
    "height": 30,
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<p>Introducing our latest addition to the mid-century inspired office furniture collection - the versatile and stylish office chair. This chair is designed to elevate any workspace, whether it be at home or in a business setting. With a variety of shell colors and base finishes to choose from, you can customize this chair to fit your aesthetic perfectly.</p>\n",
       "\n",
       "<p>The construction of this chair is top-notch, featuring a 5-wheel plastic coated aluminum base for stability and mobility. The pneumatic chair adjust allows for easy raise and lower action, ensuring maximum comfort throughout the day.</p>\n",
       "\n",
       "<p>When it comes to materials, this chair is built to last. The shell base glider is made of cast aluminum with a modified nylon PA6/PA66 coating, while the seat is crafted from HD36 foam for ultimate comfort and durability.</p>\n",
       "\n",
       "<p>Don't compromise on style or functionality - choose our mid-century inspired office chair for your workspace today.</p>\n",
       "\n",
       "<p>Product IDs: SWC-100, SWC-110</p>\n",
       "</div>\n",
       "\n",
       "<table>\n",
       "  <caption>Product Dimensions</caption>\n",
       "  <tr>\n",
       "    <td>WIDTH</td>\n",
       "    <td>20.87\"</td>\n",
       "  </tr>\n",
       "  <tr>\n",
       "    <td>DEPTH</td>\n",
       "    <td>20.08\"</td>\n",
       "  </tr>\n",
       "  <tr>\n",
       "    <td>HEIGHT</td>\n",
       "    <td>31.50\"</td>\n",
       "  </tr>\n",
       "  <tr>\n",
       "    <td>SEAT HEIGHT</td>\n",
       "    <td>17.32\"</td>\n",
       "  </tr>\n",
       "  <tr>\n",
       "    <td>SEAT DEPTH</td>\n",
       "    <td>16.14\"</td>\n",
       "  </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(HTML(response))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ed02a02-9e12-4f63-97bd-f52064b18666",
   "metadata": {},
   "source": [
    "## Try experimenting on your own!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbe1f850",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
